﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ERP_RORZE_SYSTEM;

namespace ERP_RORZE_DLL.Business
{
    public partial class panel_ReportPOCover : UserControl
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        
        public panel_ReportPOCover()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            LoadPOID();
            this.reportType.SelectedIndex = 0;
        }
        
        private void LoadPOID()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT ID FROM [241_PO] WHERE Cancelled=0 AND Change=0 ORDER BY ID";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["ID"].ToString();
                    item.Value = dataReader["ID"].ToString();
                    this.poID.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show("Cannot get POID list");
            }
        } 

        private void buttonView_Click(object sender, EventArgs e)
        {            
            if (this.poID.SelectedIndex < 0)
            {
                MessageBox.Show("Please select POID to view report");
            }
            else
            { 
                ComboboxItem item = (ComboboxItem)poID.SelectedItem;

                switch (this.reportType.SelectedIndex)
                {
                    case 0:
                        reportDocument.DocumentSource = null;
                        rpt_POCover POCover = new rpt_POCover();
                        POCover.po_id = item.Value.ToString();                        
                        POCover.LoadData();
                        POCover.CreateDocument();
                        reportDocument.DocumentSource = POCover;                        
                        break;
                    case 1:
                        break;
                } 
            }  
        }
    }
}
